/*
 * MidiMenuManager.cpp
 *
 *  Created on: 25/lug/2011
 *      Author: marcolembo
 */


#ifndef _WP_
#define _WP_
#include "WProgram.h"
#endif

#include "MidiMenuManager.h"

namespace ml
{


    MidiMenuManager* MidiMenuManager::s_mm = 0;

    const char* MidiMenuManager::sonar_lev1 = "Ultrasonic";
    const char* MidiMenuManager::sonar_lev2_changeMidiCh = "Sonar_canaleMidi";
    const char* MidiMenuManager::sonar_lev3_changeMidiChEdit = "Sonar_canaleMidi_Edit";
    const char* MidiMenuManager::sonar_lev2_changeMidiMode = "Sonar_modoMidi";
    const char* MidiMenuManager::sonar_lev3_changeMidiModeEdit = "Sonar_modoMidi_Edit";

    const char* MidiMenuManager::footsw1_lev1 = "Footswitch 1";
    const char* MidiMenuManager::footsw2_lev1 = "Footswitch 2";
    const char* MidiMenuManager::fw1_lev2_changeMidiCh = "FW1_canaleMidi";
    const char* MidiMenuManager::fw1_lev3_changeMidiChEdit = "FW1_canaleMidi_Edit";
    const char* MidiMenuManager::fw1_lev2_changeMidiMode = "FW1_modoMidi";
    const char* MidiMenuManager::fw1_lev3_changeMidiModeEdit = "FW1_modoMidi_Edit";

    MidiMenuManager* MidiMenuManager::GetInstance(cb_use use, cb_change change)
    {
        if(!s_mm)
            s_mm = new MidiMenuManager(use, change);

        return s_mm;
    }


    MidiMenuManager::MidiMenuManager(cb_use use, cb_change change)
    {
        Init(use, change);
    }

    MidiMenuManager::~MidiMenuManager()
    {
        delete m_mainMenu;
    }


    void MidiMenuManager::Init(cb_use use, cb_change change)
    {

        m_mainMenu = new MenuBackend(use, change);

        MenuItem item1 = MenuItem(sonar_lev1);
        MenuItem item2 = MenuItem(footsw1_lev1);
        MenuItem item3 = MenuItem(footsw2_lev1);

          MenuItem item11 = MenuItem(sonar_lev2_changeMidiCh);
            //  MenuItem item111 = MenuItem(sonar_lev3_changeMidiChEdit); //scendo e modifico
          MenuItem item12 = MenuItem(sonar_lev2_changeMidiMode);


          MenuItem item21 = MenuItem(fw1_lev2_changeMidiCh);
          MenuItem item22 = MenuItem(fw1_lev2_changeMidiMode);

          MenuItem item31 = MenuItem("Item3SubItem1");
          MenuItem item32 = MenuItem("Item3SubItem2");

          //configure menu
          Serial.println("Setting up menu...");
          m_mainMenu->getRoot().add(item1);
          item1.addRight(item2).addRight(item3); //livello 1

          item1.add(item11).addRight(item12);    //livello 2
          item2.add(item21).addRight(item22);
          item3.add(item31).addRight(item32);

     //    item11.addRight(item111); //edit midi ch

          m_mainMenu->toRoot();

    }

/*
    void MenuManager::CheckControlsAndNavigateMenus()
    {
        MenuItem currentMenu = m_mainMenu->getCurrent();

        if(lastButtonPushed == m_buttonPinEnter)
        {
            if(!(currentMenu.moveDown()))
            {  //if the current menu has a child and has been pressed enter then menu navigate to item below
                //qui si scatena la chiamata di callback--->su tasto ENTER!!
                m_mainMenu->use();
            }
            else
            {  //otherwise, if menu has no child and has been pressed enter the current menu is used
                m_mainMenu->moveDown();
            }
        }
        else if(lastButtonPushed == m_buttonPinEsc)
        {
            m_mainMenu->toRoot();  //back to main
        }
        else if(lastButtonPushed == m_buttonPinRight)
        {
            m_mainMenu->moveRight();
        }
        else if(lastButtonPushed == m_buttonPinLeft)
        {
            m_mainMenu->moveLeft();
        }

        lastButtonPushed = 0; //reset the lastButtonPushed variable

    }

    void MenuManager::ReadButtons()
    {
        int reading;
        int buttonEnterState=LOW;             // the current reading from the Enter input pin
        int buttonEscState=LOW;               // the current reading from the input pin
        int buttonLeftState=LOW;              // the current reading from the input pin
        int buttonRightState=LOW;             // the current reading from the input pin

        //Enter button
            // read the state of the switch into a local variable:
            reading = digitalRead(m_buttonPinEnter);

            // check to see if you just pressed the enter button
            // (i.e. the input went from LOW to HIGH),  and you've waited
            // long enough since the last press to ignore any noise:

            // If the switch changed, due to noise or pressing:
            if (reading != lastButtonEnterState) {
              // reset the debouncing timer
              lastEnterDebounceTime = millis();
            }

            if ((millis() - lastEnterDebounceTime) > debounceDelay) {
              // whatever the reading is at, it's been there for longer
              // than the debounce delay, so take it as the actual current state:
              buttonEnterState=reading;
              lastEnterDebounceTime=millis();
            }

            // save the reading.  Next time through the loop,
            // it'll be the lastButtonState:
            lastButtonEnterState = reading;


          //Esc button
            // read the state of the switch into a local variable:
            reading = digitalRead(m_buttonPinEsc);

            // check to see if you just pressed the Down button
            // (i.e. the input went from LOW to HIGH),  and you've waited
            // long enough since the last press to ignore any noise:

            // If the switch changed, due to noise or pressing:
            if (reading != lastButtonEscState) {
              // reset the debouncing timer
              lastEscDebounceTime = millis();
            }

            if ((millis() - lastEscDebounceTime) > debounceDelay) {
              // whatever the reading is at, it's been there for longer
              // than the debounce delay, so take it as the actual current state:
              buttonEscState = reading;
              lastEscDebounceTime=millis();
            }

            // save the reading.  Next time through the loop,
            // it'll be the lastButtonState:
            lastButtonEscState = reading;


         //Down button
            // read the state of the switch into a local variable:
            reading = digitalRead(m_buttonPinRight);

            // check to see if you just pressed the Down button
            // (i.e. the input went from LOW to HIGH),  and you've waited
            // long enough since the last press to ignore any noise:

            // If the switch changed, due to noise or pressing:
            if (reading != lastButtonRightState) {
              // reset the debouncing timer
              lastRightDebounceTime = millis();
            }

            if ((millis() - lastRightDebounceTime) > debounceDelay) {
              // whatever the reading is at, it's been there for longer
              // than the debounce delay, so take it as the actual current state:
              buttonRightState = reading;
             lastRightDebounceTime =millis();
            }

            // save the reading.  Next time through the loop,
            // it'll be the lastButtonState:
            lastButtonRightState = reading;


          //Up button
            // read the state of the switch into a local variable:
            reading = digitalRead(m_buttonPinLeft);

            // check to see if you just pressed the Down button
            // (i.e. the input went from LOW to HIGH),  and you've waited
            // long enough since the last press to ignore any noise:

            // If the switch changed, due to noise or pressing:
            if (reading != lastButtonLeftState) {
              // reset the debouncing timer
              lastLeftDebounceTime = millis();
            }

            if ((millis() - lastLeftDebounceTime) > debounceDelay) {
              // whatever the reading is at, it's been there for longer
              // than the debounce delay, so take it as the actual current state:
              buttonLeftState = reading;
              lastLeftDebounceTime=millis();;
            }

            // save the reading.  Next time through the loop,
            // it'll be the lastButtonState:
            lastButtonLeftState = reading;

            //records which button has been pressed
            if (buttonEnterState==HIGH)
            {
              lastButtonPushed=m_buttonPinEnter;
            }
            else if(buttonEscState==HIGH)
            {
              lastButtonPushed=m_buttonPinEsc;
            }
            else if(buttonRightState==HIGH)
            {
              lastButtonPushed=m_buttonPinRight;
            }
            else if(buttonLeftState==HIGH)
            {
              lastButtonPushed=m_buttonPinLeft;
            }
            else
            {
              lastButtonPushed=0;
            }
      }
*/
}
